package com.laolu.icon.utils;

import com.laolu.icon.entity.IconInfo;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.RECT;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.List;

@Slf4j
public class MyRestore {
	/**
	 * 挪走占位图标，还原图标
	 */
	public void restore(String backupFilePath, boolean createMissingIcons) throws Exception {
		MyFileRead myRead = new MyFileRead(backupFilePath);
		ShortcutUtils shortcutUtils = new ShortcutUtils();
		MyDesktop myDesktop = new MyDesktop();
		MyIcon myIcon = new MyIcon();
		
		try (myRead; myDesktop; shortcutUtils) {
			//读入备份文件
			List<IconInfo> backupIconInfo = myRead.readList(IconInfo.class);
			//获取当前桌面图标列表
			List<IconInfo> currentIconInfo = myIcon.getDesktopIconInfo(myDesktop, shortcutUtils);
			//获取桌面句柄
			HWND hDeskWnd = myDesktop.getHDeskWnd();
			
			if (createMissingIcons) {
				boolean hasMissingIcons = createMissingIcons(shortcutUtils, currentIconInfo, backupIconInfo);
				if (hasMissingIcons) {
					Thread.sleep(1000);
					currentIconInfo = myIcon.getDesktopIconInfo();
				}
			}
			
			moveIcons(myIcon, hDeskWnd, currentIconInfo, backupIconInfo);
			
		}
	}
	
	public void restore(String backupFilePath) throws Exception {
		restore(backupFilePath, true);
	}
	
	
	private void moveIcons(MyIcon myIcon, HWND hDeskWnd, List<IconInfo> currentIconInfo, @NotNull List<IconInfo> backupIconInfo) {
		//遍历备份文件中的图表信息
		for (IconInfo lastIcon : backupIconInfo) {
			
			
			//寻找当前桌面图标列表中是否存在选中的备份图标信息
			IconInfo currentIcon = MyIconInfoUtils.getIconInfoFromList
					(lastIcon, currentIconInfo, true, true, true);
			//不存在
			if (currentIcon == null) {
				continue;
			}
			//存在，判断图标位置是否发生变化
			if (!MyRectUtils.equalsRECT(lastIcon.getRect(), currentIcon.getRect())) {
				
				log.error("位置发生变化 {}", lastIcon.getTitle());
				log.error("current rect {}", currentIcon.getRect());
				log.error("last rect {}", lastIcon.getRect());
				
				//判断原位置是否被占据
				int overlayIndex = MyRectUtils.validateIconPosition(lastIcon.getRect(), currentIconInfo);
				if (overlayIndex >= 0) {
					log.info("检测到占位图标 index:{} title:{}", overlayIndex, currentIconInfo.get(overlayIndex).getTitle());
					//寻找一个空的位置用来放占位图标
					RECT emptyPosition = MyRectUtils.getEmptyPosition(currentIconInfo);
					log.info("移动占位图标到:{}", emptyPosition);
					//移动占位图标到空位置
					LRESULT ret = myIcon.setIconPosition
							(hDeskWnd, overlayIndex, emptyPosition.left, emptyPosition.top, false);
					//判断是否移动成功
					if (ret.intValue() == 1) {
						currentIconInfo.get(overlayIndex).setRect(emptyPosition);
						log.info("移动占位图标成功");
					} else {
						log.error("移动占位图标位置失败 error code {}", Kernel32.INSTANCE.GetLastError());
					}
				}
				
				//移动当前图标到原位置
				int index = currentIconInfo.indexOf(currentIcon);
				LRESULT ret = myIcon.setIconPosition
						(hDeskWnd, index, lastIcon.getRect().left, lastIcon.getRect().top, false);
				
				if (ret.intValue() == 1) {
					log.info("还原图标位置成功:{}", currentIcon.getTitle());
					currentIcon.setRect(lastIcon.getRect());
				} else {
					log.info("还原图标位置失败:{} error code:{}", currentIcon.getTitle(), Kernel32.INSTANCE.GetLastError());
				}
				
				
			}
			
		}
	}
	
	
	private boolean createMissingIcons(ShortcutUtils shortcutUtils, List<IconInfo> currentIconInfo, @NotNull List<IconInfo> backupIconInfo) {
		boolean hasMissingIcons = false;
		//遍历备份文件中的图表信息
		for (IconInfo lastIcon : backupIconInfo) {
			//寻找当前桌面图标列表中是否存在选中的备份图标信息
			IconInfo currentIcon = MyIconInfoUtils.getIconInfoFromList
					(lastIcon, currentIconInfo, true, true, true);
			//不存在
			if (currentIcon == null && lastIcon.getType().equals(IconInfo.ShortcutType.Shortcut)) {
				hasMissingIcons = true;
				log.error("快捷方式不存在 {}", lastIcon.getFullName());
				//创建快捷方式
				shortcutUtils.createShortcut(lastIcon);
				log.info("创建快捷方式 {}", lastIcon.getTitle());
			}
		}
		return hasMissingIcons;
	}
	
	
}
